package com.team.order.service.impl;

import com.team.order.domain.RepairReservation;
import com.team.order.domain.dto.RepairDTO;
import com.team.order.mapper.RepairReservationMapper;
import com.team.order.service.RepairService;
import com.team.order.utils.RedisIdWorker;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 梁国瑞
 * @version 1.0
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class RepairServiceImpl implements RepairService {

    private final RepairReservationMapper repairReservationMapper;

    private final RedisIdWorker redisIdWorker;

    @Override
    public Long addRepair(RepairReservation repairReservation) {
        long l = redisIdWorker.nextId("repair:order");
        repairReservation.setId(l);
        return repairReservationMapper.addRepair(repairReservation);
    }

    @Override
    public int updateRepair(RepairReservation repairReservation) {
        return repairReservationMapper.updateByPrimaryKeySelective(repairReservation);
    }
    
    @Override
    public List<RepairReservation> getReservationList(Long merchantId, String status) {
        // TODO: 实现查询维修预约列表的逻辑
        // 这里需要根据merchantId和status查询预约列表
        log.info("查询维修预约列表，商户ID：{}，状态：{}", merchantId, status);
        return repairReservationMapper.selectByMerchantIdAndStatus(merchantId, status);
    }
    
    @Override
    public RepairReservation getReservationById(Long reservationId) {
        // TODO: 实现根据ID获取维修预约详情的逻辑
        log.info("获取维修预约详情，预约ID：{}", reservationId);
        return repairReservationMapper.selectByPrimaryKey(reservationId);
    }
    
    @Override
    public boolean validateReservation(Long reservationId) {
        try {
            RepairReservation reservation = getReservationById(reservationId);
            if (reservation == null) {
                log.warn("维修预约记录不存在，ID：{}", reservationId);
                return false;
            }
            
            // 检查状态是否为已预约（0）或已确认（1）
            if (!"0".equals(reservation.getStatus()) && !"1".equals(reservation.getStatus())) {
                log.warn("维修预约记录状态不正确，ID：{}，状态：{}", reservationId, reservation.getStatus());
                return false;
            }
            
            log.info("维修预约记录验证通过，ID：{}，状态：{}", reservationId, reservation.getStatus());
            return true;
        } catch (Exception e) {
            log.error("验证维修预约记录异常，ID：{}", reservationId, e);
            return false;
        }
    }
    
    @Override
    public int updateReservationStatus(Long reservationId, String status) {
        try {
            RepairReservation reservation = new RepairReservation();
            reservation.setId(reservationId);
            reservation.setStatus(status);
            
            int result = updateRepair(reservation);
            if (result > 0) {
                log.info("更新维修预约状态成功，ID：{}，状态：{}", reservationId, status);
            } else {
                log.warn("更新维修预约状态失败，ID：{}，状态：{}", reservationId, status);
            }
            return result;
        } catch (Exception e) {
            log.error("更新维修预约状态异常，ID：{}，状态：{}", reservationId, status, e);
            return 0;
        }
    }
    
    @Override
    public int cancelReservation(Long reservationId) {
        return updateReservationStatus(reservationId, "3"); // 3-已取消
    }
}
